<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class LoginController extends Controller{
	public function index(){
		// p($_SESSION);
		// c4ca4238a0b923820dcc509a6f75849b=1
		if(IS_POST){
			// 判断验证码
			// p(Q("post."));
			$code=Q("post.code",'','strtoupper');
			if($code!=$_SESSION['code']) View::error("验证码输入不正确");
			// 判断用户名
			$username=Q("post.username");
			$data=Db::table('admin')->where("username='{$username}'")->get();
			// p($data);
			if(!$data) View::error("用户名或密码不正确");
			//判断密码
			$password=Q("post.password",'','md5');
			if($password !=$data[0]['password']){
				View::error("用户名或密码不正确");
			}
			//压入时间戳
			$time=time();
			Db::table('admin')->where("username='{$username}'")->update(["logintime"=>time()]);
			Db::table('admin')->where("username='{$username}'")->update(['aip'=>$_SERVER['HTTP_HOST']]);
			
			// p($time);
			
			// 把数据压入缓存
			$_SESSION['info']=[
				'aid'=>$data[0]['aid'],
				'username'=>$username,
				'logintime'=>$time,
				'aip'=>$_SERVER['HTTP_HOST']
			];
			// p($_SESSION);
			// exit;
			// p($data);
			View::success('登录成功',U('Index/index'));
		}
		
		View::make();
	}
	public function code(){
		Code::width(100)->height(40)->num(1)->make();
	}
	public function out(){
    	session_unset();
    	session_destroy();
    	echo "<script>window.parent.location.href='".U('Login/index')."'</script>";
    	// go(U('Login/index'));
    }
	
}